﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;

public partial class ShopKipsData : ExtBaseRpt
{
    mrjapp mrj = new mrjapp();
    string mid;
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            BuildGridPanel(this.GridPanel1, mid, Session["UserID"].ToString());
            this.comShop.GetStore().DataSource = mydb.DM_GetDateSet("select * from bas_mrj_shop");

            this.txtqbdate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            this.txtqedate.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }
    }
    protected void BtnQureyUser(object sender, DirectEventArgs e)
    {
        string str = "";
        string shop = StrSplitParam(comShop);
        shop = shop.Replace("''", "'");
        if (shop != "")
        {
            str += " and s.mrjshopid in" + shop;
        }
        if (txtshop.Text != "")
        {
            str += " and mrjshopname like '%" + txtshop.Text + "%'";
        }
        string sql = @"select a.*,s.mrjshopname from bas_kpiesdata a 
                       inner join bas_mrj_shop s on s.mrjshopid=a.shopid where cast(convert(varchar(20),kpidate,23)as datetime) between '"
            + this.txtqbdate.Text + "' and '" + this.txtqedate.Text + "' " + str + " and kpiid =" + comState.Value + " order by kpidate";
        QueryData(this.Store1, mid, sql);
    }

    protected void BtnQureyData(object sender, DirectEventArgs e)
    {
       //获取数据
        string sql = "";
        sql = "select top 10 * from bas_kpiesdata where cast(convert(varchar(20),kpidate,23)as datetime) between '" + this.txtqbdate.Text + "' and '" + this.txtqedate.Text + "'";
        if (mydb.DM_GetDateSet(sql).Tables[0].Rows.Count > 0)
        {
            ShowBox("当天数据已获取，重新获取请先删除！");
            return;
        }
        TimeSpan daycount = Convert.ToDateTime(txtqedate.Text) - Convert.ToDateTime(txtqbdate.Text);
        int count = daycount.Days;
        string date = "";
        DataSet ds = mydb.DM_GetDateSet("select * from bas_mrj_shop");
        if (ds.Tables[0].Rows.Count > 0)
        {
            string token = mrj.gettoken();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                for (int i = 0; i <= count; i++)
                {
                    date = Convert.ToDateTime(txtqbdate.Text).AddDays(i).ToString("yyyy-MM-dd");
                    mrj.getKpiesData(date, date, dr["mrjshopid"].ToString(), token);
                }
            }
        }
        //BtnQureyUser(null, null);
        ShowBox("获取成功！");
    }
    protected void BtnDeleteData(object sender, DirectEventArgs e)
    {
        //获取数据
        string sql = "";
        sql = "delete from bas_kpiesdata where cast(convert(varchar(20),kpidate,23)as datetime) between '" + this.txtqbdate.Text + "' and '" + this.txtqedate.Text + "'";
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            ShowBox("删除成功！");
            return;
        }
        BtnQureyUser(null, null);
    }
    protected void BtnImprtPos(object sender, DirectEventArgs e)
    {
        //上传POS数据
        TimeSpan daycount = Convert.ToDateTime(txtqedate.Text) - Convert.ToDateTime(txtqbdate.Text);
        int count =daycount.Days;
        string date = "";
        for (int i = 0; i <= count; i++)
        {
            date = Convert.ToDateTime(txtqbdate.Text).AddDays(i).ToString("yyyy-MM-dd");
            ImportSaleDataByShop(date);
        }
        ShowBox("导入成功！");
    }

    public void ImportSaleDataByShop(string date)
    {
        string sql = @"select a.*,b.salecount from (
	                    select a.shopid,mrjshopid,cast(sum(a.payamount)as int)as amount,sum(qty)as qty from V_PosSale a 
	                    inner join Bas_mrj_Shop s on s.kmshopid = a.shopid
	                    where convert(varchar(20),a.billdate,23)='" + date + @"'
	                    group by shopid,mrjshopid
                    ) a 
                    inner join (
                    select shopid,count(billno)as salecount from (
	                    select distinct a.shopid,a.billno from V_PosSale a 
	                    inner join Bas_mrj_Shop s on s.kmshopid = a.shopid
	                    where convert(varchar(20),a.billdate,23)='" + date + @"' 
                    )m
                    group by shopid
                    ) b on b.shopid = a.shopid";
        try
        {
            DataSet ds = mydb.DM_GetDateSet(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    mrj.ImportshopSaleData(dr["mrjshopid"].ToString(), date, dr["amount"].ToString(), dr["salecount"].ToString(), dr["qty"].ToString());
                }
            }
        }
        catch (Exception ex)
        {
            ShowBox(ex.Message);
        }

    }
    protected void RefreshDataSet(object sender, StoreReadDataEventArgs e)
    {
        BtnQureyUser(null,null);
    }
    protected void Store1_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        //导出EXCEL
        ExtGridToExcel(mid,"xls",e.Xml,this.Response,this.Title);
    }
}